$(document).ready(function(){
	//添加
	$("#add").click(function(){
		var url=$(this).attr('url');
		var name=$("#name").val();
		var upid=$(this).attr('upid');
		var add_type=$(this).attr("add_type");
		if($.trim(name).length>0){
			$.post(
				url,
				{name:name,upid:upid},
				function(data){
					if(data.error=='1'){
						alert(data.content);
						$("#name").val("");
					}else{
						if(add_type=="top") $("#top").after(data.content);
						if(add_type=="end") $("#addTr").before(data.content);
						$("#name").val("");
					}
				}
				,'json'
			)
		}else{
			alert('内容不能为空');
			return false;
		}
	});

	//全选 hplus
	$("input[name=checkAll]").click(function(){
		var obj = $(this);
		if(obj.is(":checked")){
			$("input[name=checkbox]").prop("checked",true);
		}else{
			$("input[name=checkbox]").prop("checked",false);
		}
	});
	$("input[name=checkbox]").click(function(){
		var off = $("input[name=checkbox]");
		var i = 0;
		off.each(function(){
			if(!$(this).is(":checked")){
				i++;
			}
		})
		if(i != 0){
			$("input[name=checkAll]").prop("checked",false);
		}else{
			$("input[name=checkAll]").prop("checked",true);
		}
	});

	//hplus
	$(".btn-white").on("click",function(){
        var parent = $(window.parent.document);
        var curl = $(this).attr("curl");
       	parent.find(".page-tabs-content").children("[data-id]").not(":first").each(function(){
            parent.find('.J_iframe[data-id="'+curl+'"]').remove();
            if($(this).data("id") == curl){
            	$(this).remove()
            }
        });
        parent.find(".page-tabs-content").children("[data-id]:last").each(function(){
            parent.find('.J_iframe[data-id="'+$(this).data("id")+'"]').show();
            $(this).addClass("active")
        });
        $(".page-tabs-content").css("margin-left","0")
    })
})

//修改 hplus
function edit(name_id,obj,url){
	var td=$("td[name_id="+name_id+"]");
	var name=td.text();
	td.html('<input type="text" value="'+name+'" name_id="'+name_id+'"/>');
	obj.attr('onclick',"save('"+name_id+"',$(this),'"+url+"')");
	obj.text('保存');
}

//保存 hplus
function save(name_id,obj,url){
	var td=$("td[name_id="+name_id+"]");
	var name=$("input[name_id="+name_id+"]").val();
	$.post(
		url,
		{name_id:name_id,name:name},
		function(data){
			if(data=='1'){
				td.html(name);
				obj.prop('onclick',"edit('"+name_id+"',$(this),'"+url+"')");
				obj.text("修改");
			}
		}
	)
}

//全选
function allcheck(){
	$("input:[name=checkbox]").attr("checked",true);
}

//删除  hplus
function del(url){
	var id="";
	$("input[name=checkbox]").each(function(){
		if($(this).is(":checked")){
			id+=$(this).val()+",";
		}
	});
	if(id == ''){
		alert("请选择");
		return false;
	}
	$.post(
		url,
		{id:id},
		function(data){
			if(data=='1'){
				$("input[name=checkbox]").each(function(){
					if($(this).is(":checked")){
						$(this).parent().parent().remove();
					}
				})
			}
		}
	)
}

//修改状态
function state(id,key,url,obj){
	$.post(
		url+"?action=now",
		{id:id,key:key},
		function(data){
			obj.css('color',data.color);
			obj.html(data.name);
		},
		'json'
	)
}

//输入框闪烁
var Color= ["FEE","FDD","FCC","FBB","FAA","FEE","FDD","FCC","FBB","FAA","FFF"];

function fadeIn(obj,where) {
	if (where >= 0) {
		$(obj).css("background","#" + Color[where]);
		where -= 1;
		setTimeout("fadeIn('"+obj+"',"+where+")", 50);
	} else {
		setTimeout("fadeOut('"+obj+"',0)", 50);
	}
}
function fadeOut(obj,where) {
	if (where <= Color.length) {
		$(obj).css("background","#" + Color[where]);
		where += 1;
		setTimeout("fadeOut('"+obj+"',"+where+")", 50)
	}
}

//简单验证
function queding(msg,inputClass){
	var is_null = true;
	$(inputClass).each(function(i){
		if($(this).val()==""){
			var checkMsg = $(this).attr("checkMsg");
			if(checkMsg!=undefined && checkMsg!=''){
				alert(checkMsg);
			}
			fadeIn(inputClass+":eq("+i+")",0);
			is_null = false;
			return false;
		}
	})
	if(is_null){
		var r = confirm(msg);
		return r;
	}else{
		return false;
	}
}

//排序
function sort(obj,id,type,url){
	$.post(
		url,
		{id:id,type:type},
		function(data){
			var tr = obj.parent().parent();
			var trHtml = "<tr>"+tr.html()+"</tr>";
			if(type=="up"){
				var prev = tr.prev();
				if(prev.attr("id")!='top'){
					prev.before(trHtml);
				}
				if(data!='')tr.remove();
			}else if(type=="down"){
				var next = tr.next();
				if(next.text()!='' && next.attr('id')!="addTr"){
					next.after(trHtml);
				}
				if(data!='')tr.remove();
			}
		}
	)
}